package com.itheima.project_cloth.aop;


import com.itheima.project_cloth.pojo.Emp;
import com.itheima.project_cloth.mapper.LoginLogmapper;
import com.itheima.project_cloth.pojo.EmpLoginLog;
import com.itheima.project_cloth.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Aspect
@Component
@Slf4j
public class LoginLog {

    @Autowired
    private LoginLogmapper loginLogmapper;



    @Around("execution(* com.itheima.project_cloth.controller.LoginController.Login(..))")
    public  Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable {


        Object[] args = joinPoint.getArgs();
        String username = ((Emp) args[0]).getUserName();
        String password = ((Emp) args[0]).getPassword();


        LocalDateTime loginTime = LocalDateTime.now();

        long bagin=System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        Result res = (Result) proceed;

        String jwt = (String) res.getData();

        Short isSuccess;

        if (res.getCode()==1) {
            isSuccess=1;
        }else {
            isSuccess=0;
        }
        long end=System.currentTimeMillis();
        long costTime=end-bagin;

        EmpLoginLog log = new EmpLoginLog(null,username,password,loginTime,isSuccess,jwt,costTime);
        loginLogmapper.insert(log);
        return proceed;
    }
}
